/**
 * Created with IntelliJ IDEA
 * Description:
 * User: lenovo
 * Date: 2023 -04 -15
 * Time: 14:03
 */
public class MyCircularQueue {
    private int[] elem;
//注意此方法采用的是”浪费一个空间“的方法，所以实际上这里只能存放(k-1）个元素
    public MyCircularQueue(int k) {
        elem = new int[k];
    }

    private int front;
    private int rear;

    //入队
    public boolean enQueue(int value) {

        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //出队
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }//注意这里要对rear的位置进行一个判断，如若理解不了，尝试画图理解
        if (rear == 0) {
            return elem[elem.length - 1];
        }
        return elem[rear - 1];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return ((rear + 1) % elem.length == front);
    }
}
